<?
require_once('version.inc');
require_once('dbaccess.inc');
require_once('language.inc');
require_once('header.inc');
require_once('crypt.inc');
require_once('settings.inc');
require_once('menu.inc');

function SaveNewAbt() {
	$ka_num = $_REQUEST['ka_num'];
	$ka_abt = $_REQUEST['ka_abt'];

	$db = OpenDB();
	$query = "select ka_abt from key_abt where ka_num = $ka_num";
	$result = QueryDB($db, $query);
	$numrows = numrowsDB($result);

	if ($numrows == 1) {
	   Error("Schl&uuml;ssel $ka_num ist bereits vorhanden!\n");
	   closeDB($db);
	   return false;
	}

	$query = "insert into key_abt (ka_num, ka_abt) values ($ka_num, '$ka_abt')";

	if (!TQueryDB($db, $query)) {
	   Error("SQL-Error: $query");
	   closeDB($db);
	   return false;
	}

	closeDB($db);
	return true;
}

function SaveEditAbt() {
	$ka_num = $_REQUEST['ka_num'];
	$ka_abt = $_REQUEST['ka_abt'];

	$db = OpenDB();
	$query = "select ka_abt from key_abt where ka_num = $ka_num";
	$result = QueryDB($db, $query);
	$numrows = numrowsDB($result);

	if ($numrows != 1) {
	   Error("Schl&uuml;ssel $ka_num ist nicht vorhanden!\n");
	   closeDB($db);
	   return false;
	}

	$query = "update key_abt set ka_abt = '$ka_abt' where ka_num = $ka_num";

	if (!TQueryDB($db, $query)) {
	   Error("SQL-Error: $query");
	   closeDB($db);
	   return false;
	}

	closeDB($db);
	return true;
}

$newabt = $_REQUEST['newabt'];
$editabt = $_REQUEST['editabt'];

if (!isset($editabt)) {
   $editabt = 0;
}

$savenewabt = $_REQUEST['savenewabt'];
$saveeditabt = $_REQUEST['saveeditabt'];

if (isset($savenewabt)) {
   SaveNewAbt();
}

if (isset($saveeditabt)) {
   SaveEditAbt();
}

?>
<form name="abt" action="abt.php" method="post">
<input type="hidden" name="headline" value="16">
<input type="hidden" name="rstufe" value="<? echo "$rstufe"; ?>">
<input type="hidden" name="menu" value="<? echo "$menu"; ?>">
<input type="hidden" name="unum" value="<? echo "$unum"; ?>">
<table class="indent"><tr><td>
<?
Button("Neue Abteilung", "abt");
//<button type="submit" name="newabt" value="newabt"><img src="image/button_newabt.png" border=0 alt="Neue Abteilung"></button>
?>
</td></tr></table>
<?php
$db = OpenDB();
$query = "select ka_num, ka_abt from key_abt order by ka_num";
$result = QueryDB($db, $query);

if (!$result) {
   echo "</table></form><br>\n";
   Error("SQL-Error: $query");
   require('footer.inc');
   exit;
}

$numrows = numrowsDB($result);

if ($numrows > 0 || isset($newabt)) {
?>
<table class="sel">
   <tr>
      <th class="sel">Aktion</th>
      <th class="sel">Key</th>
      <th class="sel">Text</th>
   </tr>
<?php
   $row = 0;

   while ($row < $numrows) {
      $data = fetchDB($result, $row);
      $ka_num = $data[0];
      $ka_abt = $data[1];

      if ($ka_num == $editabt) {
         echo "<input type=\"hidden\" name=\"ka_num\" value=\"$ka_num\">\n";
         echo "<tr><td class=\"selakt\"><button type=\"submit\" name=\"saveeditabt\" value=\"$ka_num\"><img src=\"image/new.png\" border=0 alt=\"Edit\"></button></td>";
         echo "<td class=\"selnum\">$ka_num</td>";
         echo "<td class=\"sel\"><input type=\"text\" class=\"inputmust\" name=\"ka_abt\" value=\"$ka_abt\" size=40 maxlength=50></td></tr>\n";
      } else {
         echo "<tr><td class=\"sel\"><button type=\"submit\" name=\"editabt\" value=\"$ka_num\"><img src=\"image/edit.png\" border=0 alt=\"Edit\"></button></td>";
         echo "<td class=\"selnum\">$ka_num</td><td class=\"sel\">$ka_abt</td></tr>";
      }

      $row++;
   }

   if (isset($newabt)) {
      echo "<tr><td class=\"sel\"><button type=\"submit\" name=\"savenewabt\" value=\"savenewabt\"><img src=\"image/new.png\" border=0 alt=\"Neu\"></button></td>";
      echo "<td class=\"sel\"><input type=\"text\" class=\"inputmust\" name=\"ka_num\" size=7 maxlength=7></td>";
      echo "<td class=\"sel\"><input type=\"text\" class=\"inputmust\" name=\"ka_abt\" size=40 maxlength=50></td></tr>\n";
   }
?>
</table>
<table class="indent"><tr><td>
<?
   Button("Neue Abteilung", "abt");
//<button type="submit" name="newabt" value="new"><img src="image/button_newabt.png" border=0 alt="Neue Abteilung"></button>
?>
</td></tr></table>
</form>
<?php
}

require('footer.inc');
?>
